package com.zyy.smartcity1129.news;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.homeLbInfo;
import com.zyy.smartcity1129.data.newslist;
import com.zyy.smartcity1129.data.newstab;
import com.zyy.smartcity1129.util.BannerAd;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.util.XCUtli;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class NewsFragment extends BaseFragment {
    private Banner bannerNew;
    private TabLayout tabNew;
    private RecyclerView rvNew;
    List<newslist.RowsBean> nList = new ArrayList<>();
    List<newslist.RowsBean> newsList = new ArrayList<>();
    List<newstab.DataBean> tabList = new ArrayList<>();
    List<homeLbInfo.RowsBean> bannerList = new ArrayList<>();
    List<Object> bList = new ArrayList<>();
    public static int newsId;
    public static String typeId;
    public static String names;

    @Override
    protected int layId() {
        return R.layout.f_news;
    }

    @Override
    protected String title() {
        return "新闻";
    }

    @Override
    protected boolean back() {
        return false;
    }

    @Override
    protected Boolean bottom() {
        return true;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        XCUtli xcUtli = new XCUtli(3);
        xcUtli.then(() -> {
            newsList = Http.getInstance().get("/prod-api/press/press/list", null, newslist.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            tabList = Http.getInstance().get("/prod-api/press/category/list", null, newstab.class, 1).getData();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            bannerList = Http.getInstance().get("/prod-api/api/rotation/list", null, homeLbInfo.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.XCliveData.observe(getViewLifecycleOwner(), atomicInteger -> {
            if (atomicInteger.get() == 3) {
                for (homeLbInfo.RowsBean b : bannerList) {
                    bList.add(b.getAdvImg());
                }
                nList.addAll(newsList.stream().filter(a -> Integer.valueOf(a.getType()) == tabList.get(0).getId()).sorted(Comparator.comparing(newslist.RowsBean::getUpdateTime)).collect(Collectors.toList()));
                RvAd<newslist.RowsBean> rvNewsAd = new RvAd<newslist.RowsBean>(nList, R.layout.rv_base) {
                    private void initView(View vi) {
                        ig = (ImageView) vi.findViewById(R.id.ig);
                        tv1 = (TextView) vi.findViewById(R.id.tv1);
                        tv2 = (TextView) vi.findViewById(R.id.tv2);
                        tv3 = (TextView) vi.findViewById(R.id.tv3);
                        tv4 = (TextView) vi.findViewById(R.id.tv4);
                        tv5 = (TextView) vi.findViewById(R.id.tv5);
                        tv5.setVisibility(View.GONE);
                    }

                    private TextView tv5;
                    private TextView tv4;
                    private TextView tv3;
                    private TextView tv2;
                    private TextView tv1;
                    private ImageView ig;

                    @Override
                    protected void initR(View vi, newslist.RowsBean rowsBean, int p) {
                        initView(vi);
                        Glide.with(ig.getContext()).load(Http.base + rowsBean.getCover()).into(ig);
                        tv1.setText(rowsBean.getTitle());
                        tv2.setText(rowsBean.getAppType());
                        tv3.setText(rowsBean.getPublishDate());
                        tv4.setText("观看人数" + rowsBean.getReadNum() + "    点赞人数：" + rowsBean.getLikeNum());
                        vi.setOnClickListener(view -> {
                            newsId = rowsBean.getId();
                            names = rowsBean.getTitle();
                            typeId = rowsBean.getType();
                            add(new News1Fragment());
                        });
                    }
                };
                tabNew.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        nList.clear();
                        int nameTypeId = tabList.stream().filter(a -> a.getName() == tab.getText().toString()).findFirst().get().getId();
                        nList.addAll(newsList.stream().filter(a -> Integer.valueOf(a.getType()) == nameTypeId).sorted(Comparator.comparing(newslist.RowsBean::getUpdateTime)).collect(Collectors.toList()));
                        rvNewsAd.notifyDataSetChanged();
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
                    }
                });
                tabNew.removeAllTabs();
                for (newstab.DataBean pl : tabList) {
                    tabNew.addTab(tabNew.newTab().setText(pl.getName()));
                }
                rvNew.setLayoutManager(new LinearLayoutManager(getContext()));
                rvNew.setAdapter(rvNewsAd);
                bannerNew.setAdapter(new BannerAd(bList)).setIndicator(new CircleIndicator(getContext()))
                        .addBannerLifecycleObserver(this).start();
                bannerNew.setOnBannerListener(((o, i) -> {
                    newsId = bannerList.get(i).getTargetId();
                    if (!(newsId == 0)){
                        names = newsList.stream().filter(a->a.getId()==bannerList.get(i).getTargetId()).findFirst().get().getTitle();
                        typeId = newsList.stream().filter(a->a.getId()==bannerList.get(i).getTargetId()).findFirst().get().getType();
                        add(new News1Fragment());
                    }
                }));
            }
        });
    }

    private void initView(View iv) {
        bannerNew = (Banner) iv.findViewById(R.id.bannerNew);
        tabNew = (TabLayout) iv.findViewById(R.id.tabNew);
        rvNew = (RecyclerView) iv.findViewById(R.id.rvNew);
    }
}
